#include <iostream>

using i64 = long long;

constexpr int N = 1E5 + 5;

int tr[N][26];
int cnt[N];
int idx = 1;

void insert(const std::string &s) {
    int p = 1;
    for (int i = 0; i < s.size(); ++i) {
        auto c = s[i] - 'a';
        auto &u = tr[p][c];
        if (!u) {
            u = ++idx;
        }
        p = u;
    }
    ++cnt[p];
}

int query(const std::string &s) {
    int p = 1;
    for (int i = 0; i < s.size(); ++i) {
        auto c = s[i] - 'a';
        auto u = tr[p][c];
        if (!u) {
            return 0;
        }
        p = u;
    }
    return cnt[p];
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    std::string s;
    for (int i = 0; i < n; ++i) {
        std::cin >> s;
        insert(s);
    }

    int m;
    std::cin >> m;

    for (int i = 0; i < m; ++i) {
        std::cin >> s;
        std::cout << query(s) << "\n";
    }

    return 0;
}